Python programming

Ingeniería Biomédica

Ph.D. Pablo Eduardo Caicedo Rodríguez

2024-08-12

Python Programming

Introduction

  • Python is a high-level, interpreted programming language
  • Easy to learn and understand
  • Versatile and widely used in:
    • Web development
    • Data analysis and science
    • Artificial intelligence and machine learning
    • Automation and scripting

Basic Syntax

  • Indentation is used to denote block-level structure
  • Variables are assigned using the = operator
  • Print output using the print() function
  • Comments: # for single-line, ''' or """ for multi-line

Data Types

  • Integers: int (e.g., 1, 2, 3)
  • Floats: float (e.g., 3.14, -0.5)
  • Strings: str (e.g., 'hello', "hello")
  • Boolean: bool (e.g., True, False)
  • List: list (e.g., [1, 2, 3], ['a', 'b', 'c'])
  • Dictionary: dict (e.g., {'name': 'John', 'age': 30})

Control Structures

  • Conditional statements:
    • if statements: if condition : code
    • elif statements: elif condition : code
    • else statements: else : code
  • Loops:
    • for loops: for variable in iterable : code
    • while loops: while condition : code

Functions

  • Reusable blocks of code
  • Take arguments and return values
  • Can be used to:
    • Organize code
    • Reduce repetition
    • Encapsulate complex logic
  • Function definition: def function_name (arguments) : code

Modules

  • Pre-written code libraries
  • Imported using the import statement
  • Examples:
    • math: mathematical functions (e.g., sin(), cos())
    • random: random number generation (e.g., randint(), uniform())
    • time: time-related functions (e.g., time(), sleep())

Exception Handling

  • Try-except blocks:
    • try block: code that might raise an exception
    • except block: code to handle the exception
  • Catching specific exceptions:
    • except ValueError : code
    • except TypeError : code
  • Raising exceptions:
    • raise ValueError(message)

Object-Oriented Programming

  • Classes:
    • Define custom data types
    • Encapsulate data and behavior
  • Objects:
    • Instances of classes
    • Have attributes (data) and methods (behavior)
  • Inheritance:
    • Create new classes based on existing ones
    • Inherit attributes and methods

Advanced Topics

  • Decorators:
    • Modify function behavior
    • Use @ symbol to apply
  • Generators:
    • Special type of iterable
    • Use yield statement to define
  • Lambda functions:
    • Small, anonymous functions
    • Use lambda keyword to define

Decorators

def my_decorator(func):
    def wrapper():
        print("Something is happening before the function is called.")
        func()
        print("Something is happening after the function is called.")

    return wrapper


@my_decorator
def say_hello():
    print("Hello!")


say_hello()
Something is happening before the function is called.
Hello!
Something is happening after the function is called.

Generators

def infinite_sequence():
    num = 0
    while True:
        yield num
        num += 1

gen = infinite_sequence()
print(next(gen))
0
print(next(gen))
1
print(next(gen))
2
calcular_area_rectangulo = lambda base, altura: base * altura
print(calcular_area_rectangulo(4, 6))  # Salida: 24
24

Conclusion

  • Python is a powerful and versatile language
  • Continuously learning and practicing will help you master it
  • Explore advanced topics and libraries to become proficient